home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Hamster.EXE / hmessage.hsm < prev    next >
Encoding:
Text File  |  2000-07-22  |  6.4 KB  |  195 lines

  1. ########################################################################
  2. # Module     : hmessage.hsm
  3. # Description: Functions for messages (news, mails) loaded into a list.
  4. # Maintainer : Juergen Haible <juergen.haible@t-online.de>
  5. # Version    : 2000-07-22
  6. ########################################################################
  7. # NOTE:
  8. # This module is delivered with Hamster and it will be overwritten when
  9. # installing a new version of it. It might also be used by accompanying
  10. # demo-scripts, which depend on the current implementation.
  11. # So do NOT change this file unless you REALLY know what you do!
  12. ########################################################################
  13.  
  14. #!load hstrings.hsm
  15.  
  16. #!initialize
  17.    debug( 255, "<<< module 'hmessage.hsm' >>>" )
  18. return( 0 )
  19.  
  20. ########################################################################
  21. # MsgHeadernameOfIndex: Returns header-name of a given line.
  22. ########################################################################
  23. # [IN]  $Msg    : list containing the message
  24. #       $Idx    : index within list pointing to a header-line
  25. # [OUT] (result): header-name including colon; "" if invalid
  26. # Example: $HdrNam = MsgHeadernameOfIndex( $Msg, 2 )
  27.  
  28. sub MsgHeadernameOfIndex( $Msg, $Idx )
  29.    var( $s, $i )
  30.    $s = ListGet( $Msg, $Idx )
  31.    $i = Pos( ":", $s )
  32.    $s = iif( $i>0, copy($s,1,$i), "" )
  33.    return( $s )
  34. endsub
  35.  
  36. ########################################################################
  37. # MsgIndexOfHeader: Returns index of a given header
  38. ########################################################################
  39. # [IN]  $Msg   : list containing the message
  40. #       $HdrNam: name of header including colon
  41. # [OUT] (result): index, -1 if not found
  42. # Example: $HdrIdx = MsgIndexOfHeader( $Msg, "From:" )
  43.  
  44. sub MsgIndexOfHeader( $Msg, $HdrNam )
  45.    var( $i, $Idx )
  46.    
  47.    $Idx = -1
  48.    $i = 0
  49.    $HdrNam = LowerCase( $HdrNam )
  50.  
  51.    while( $i < ListCount($Msg) )
  52.       break( ListGet($Msg,$i) = "" ) # end of header
  53.       if( $HdrNam = LowerCase( MsgHeadernameOfIndex( $Msg, $i ) ) )
  54.          $Idx = $i
  55.          break
  56.       endif
  57.  
  58.       inc( $i )
  59.    endwhile
  60.    
  61.    return( $Idx )
  62.    
  63. endsub
  64.  
  65. ########################################################################
  66. # MsgGetHeader: Returns value of a given header
  67. ########################################################################
  68. # [IN]  $Msg   : list containing the message
  69. #       $HdrNam: name of header including colon
  70. # [OUT] (result): value of header, "" if not found
  71. # Example: $HdrVal = MsgGetHeader( $Msg, "From:" )
  72.  
  73. sub MsgGetHeader( $Msg, $HdrNam )
  74.    var( $HdrVal, $Idx, $i, $s )
  75.    
  76.    # find position of header   
  77.    $Idx = MsgIndexOfHeader( $Msg, $HdrNam )
  78.  
  79.    # if found, return value of header
  80.    if( $Idx >= 0 )
  81.       $HdrVal = ListGet( $Msg, $Idx )
  82.       $i = Pos( ":", $HdrVal )
  83.       $HdrVal = iif( $i=0, "", copy($HdrVal,$i+1) )
  84.       $s = copy( $HdrVal, 1, 1 )
  85.       if( Pos($s,$WSP) > 0 )
  86.          $HdrVal = Delete( $HdrVal, 1, 1 )
  87.       endif
  88.       
  89.       # append continuation lines separated by CR+LF
  90.       do
  91.          inc( $Idx )
  92.          $s = copy( ListGet( $Msg, $Idx ), 1, 1 )
  93.          break( Pos($s,$WSP)=0 ) # 1st char=WSP?
  94.          $HdrVal = $HdrVal + $CRLF + ListGet( $Msg, $Idx )
  95.       loop
  96.    else
  97.       $HdrVal = ""
  98.    endif
  99.  
  100.    # return value of header (i.e. not including header-name)   
  101.    return( $HdrVal )
  102.    
  103. endsub
  104.  
  105. ########################################################################
  106. # MsgDelHeader: Deletes a given header-line.
  107. ########################################################################
  108. # [IN]  $Msg   : list containing the message
  109. #       $HdrNam: name of header including colon
  110. # [OUT] (result): former index of deleted line, -1 if not found
  111. # Example: $OldIdx = MsgDelHeader( $Msg, "X-Posting-Agent:" )
  112.  
  113. sub MsgDelHeader( $Msg, $HdrNam )
  114.    var( $HdrVal, $Idx, $i, $s )
  115.  
  116.    # find position of header   
  117.    $Idx = MsgIndexOfHeader( $Msg, $HdrNam )
  118.    
  119.    # if found, delete it and its continuation lines
  120.    if( $Idx >= 0 )
  121.       ListDelete( $Msg, $Idx )
  122.       do
  123.          $s = copy( ListGet( $Msg, $Idx ), 1, 1 )
  124.          break( Pos($s,$WSP)=0 ) # 1st char=WSP?
  125.          ListDelete( $Msg, $Idx )
  126.       loop
  127.    endif
  128.  
  129.    # return old position or -1 for 'not found'   
  130.    return( $Idx )
  131.    
  132. endsub
  133.  
  134. ########################################################################
  135. # MsgAddHeader: Adds a header-line, EVEN if message already contains one
  136. #               with the given name (see also: MsgSetHeader)
  137. ########################################################################
  138. # [IN]  $Msg   : list containing the message
  139. #       $HdrNam: name of header including colon
  140. #       $HdrVal: value of header
  141. # [OUT] (result): index of new header-line, -1 on error
  142. # Example: $AddIdx = MsgAddHeader( $Msg, "X-I-Like-X-Headers:", "No" )
  143.  
  144. sub MsgAddHeader( $Msg, $HdrNam, $HdrVal )
  145.    var( $i, $Idx )
  146.    
  147.    # find header/body-separator
  148.    $Idx = ListIndexOf( $Msg, "" ) 
  149.  
  150.    # if missing, add header/body-separator
  151.    if( $Idx < 0 )
  152.       ListInsert( $Msg, 0, "" ) 
  153.       $Idx = 0
  154.    endif
  155.    
  156.    # add header-line right before header/body-separator
  157.    ListInsert( $Msg, $Idx, $HdrNam + " " + $HdrVal )
  158.    
  159.    # return postion of new header-line
  160.    return( $Idx )
  161.    
  162. endsub
  163.  
  164. ########################################################################
  165. # MsgSetHeader: Adds a header-line. If message already contains one with
  166. #               the given name, it will be replaced with the new one.
  167. ########################################################################
  168. # [IN]  $Msg   : list containing the message
  169. #       $HdrNam: name of header including colon
  170. #       $HdrVal: value of header
  171. # [OUT] (result): index of new/updated header-line, -1 on error
  172. # Example: $AddIdx = MsgSetHeader( $Msg, "X-I-Like-X-Headers:", "No" )
  173.  
  174. sub MsgSetHeader( $Msg, $HdrNam, $HdrVal )
  175.    var( $Idx )
  176.    
  177.    # delete existing header-line; save old position
  178.    $Idx = MsgDelHeader( $Msg, $HdrNam )
  179.    
  180.    # add new header-line
  181.    if( $Idx>=0 )
  182.       # insert at old position
  183.       ListInsert( $Msg, $Idx, $HdrNam + " " + $HdrVal )
  184.    else
  185.       # append to headers
  186.       $Idx = MsgAddHeader( $Msg, $HdrNam, $HdrVal )
  187.    endif
  188.    
  189.    # return postion of new/changed header-line
  190.    return( $Idx )
  191.    
  192. endsub
  193.  
  194. ########################################################################
  195.